package com.lvmama.comm.sync.pojo;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "root")
public class TemplatesRoot implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1663682654810509922L;
	
	private List<Template> templates;

	@XmlElementWrapper(name = "templates")
	@XmlElement(name = "template")
	public List<Template> getTemplates() {
		return templates;
	}

	public void setTemplates(List<Template> templates) {
		this.templates = templates;
	}

	public void response(OutputStream os) throws Exception {
		JAXBContext context = JAXBContext.newInstance(TemplatesRoot.class);
		Marshaller marshaller = context.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
		marshaller.marshal(this, os);
	}
	
	public static TemplatesRoot getInstance(InputStream is) throws Exception {
		JAXBContext context = JAXBContext.newInstance(TemplatesRoot.class);
		Unmarshaller unmarshaller = context.createUnmarshaller();
		return (TemplatesRoot) unmarshaller.unmarshal(is);
	}
}
